package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.SysHotelFeatures;
import com.ruoyi.system.domain.SysHotelRoom;
import com.ruoyi.system.domain.vo.HotelDetailsResp;
import com.ruoyi.system.domain.vo.SysHotelResp;
import com.ruoyi.system.domain.vo.SysHotelRoomResp;
import com.ruoyi.system.enums.HotelRoomTypEnum;
import com.ruoyi.system.service.ISysHotelFeaturesService;
import com.ruoyi.system.service.ISysHotelRoomService;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysHotelMapper;
import com.ruoyi.system.domain.SysHotel;
import com.ruoyi.system.service.ISysHotelService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 酒店Service业务层处理
 * 
 * @author dht
 * @date 2025-04-28
 */
@Service
public class SysHotelServiceImpl implements ISysHotelService 
{
    @Autowired
    private SysHotelMapper sysHotelMapper;
    @Autowired
    private ISysHotelRoomService sysHotelRoomService;
    @Autowired
    private ISysHotelFeaturesService sysHotelFeaturesService;

    /**
     * 查询酒店
     * 
     * @param hotelId 酒店主键
     * @return 酒店
     */
    @Override
    public SysHotel selectSysHotelByHotelId(Long hotelId)
    {
        return sysHotelMapper.selectSysHotelByHotelId(hotelId);
    }

    /**
     * 查询酒店列表
     * 
     * @param sysHotel 酒店
     * @return 酒店
     */
    @Override
    public List<SysHotelResp> selectSysHotelList(SysHotel sysHotel)
    {
        return sysHotelMapper.selectSysHotelList(sysHotel)
                .stream().map(this::buildHotelResp).collect(Collectors.toList());
    }

    @NotNull
    private SysHotelResp buildHotelResp(SysHotel item) {
        SysHotelResp resp = BeanUtil.copyProperties(item, SysHotelResp.class);
        resp.setFeatures(sysHotelFeaturesService.getByHotelId(item.getHotelId())
                .stream().map(SysHotelFeatures::getFeaturesId)
                .collect(Collectors.toList()));
        return resp;
    }

    /**
     * 新增酒店
     * 
     * @param sysHotel 酒店
     * @return 结果
     */
    @Override
    @Transactional
    public int insertSysHotel(SysHotel sysHotel)
    {
        checkIsExist(sysHotel.getName());
        sysHotel.setCreateTime(DateUtils.getNowDate());
        int i = sysHotelMapper.insertSysHotel(sysHotel);
        for (HotelRoomTypEnum item : HotelRoomTypEnum.values()) {
            sysHotelRoomService.insertSysHotelRoom(SysHotelRoom.buildDefault(item.getKey(), sysHotel.getHotelId()));
        }
        return i;
    }

    private void checkIsExist(String name) {
        SysHotel find = sysHotelMapper.selectSysHotelByHotelName(name);
        if (Objects.nonNull(find)) {
            throw new GlobalException("酒店已经存在");
        }
    }

    /**
     * 修改酒店
     * 
     * @param sysHotel 酒店
     * @return 结果
     */
    @Override
    public int updateSysHotel(SysHotel sysHotel)
    {
        sysHotel.setUpdateTime(DateUtils.getNowDate());
        return sysHotelMapper.updateSysHotel(sysHotel);
    }

    /**
     * 批量删除酒店
     * 
     * @param hotelIds 需要删除的酒店主键
     * @return 结果
     */
    @Override
    public int deleteSysHotelByHotelIds(Long[] hotelIds)
    {
        return sysHotelMapper.deleteSysHotelByHotelIds(hotelIds);
    }

    /**
     * 删除酒店信息
     * 
     * @param hotelId 酒店主键
     * @return 结果
     */
    @Override
    public int deleteSysHotelByHotelId(Long hotelId)
    {
        return sysHotelMapper.deleteSysHotelByHotelId(hotelId);
    }

    @Override
    public HotelDetailsResp getDetails(Long hotelId) {
        SysHotel sysHotel = sysHotelMapper.selectSysHotelByHotelId(hotelId);
        if (Objects.isNull(sysHotel)) {
            throw new GlobalException("酒店不存在");
        }
        HotelDetailsResp resp = new HotelDetailsResp();
        resp.setHotel(buildHotelResp(sysHotel));
        resp.setRooms(sysHotelRoomService.listByHotelId(hotelId));

        return resp;
    }

    @Override
    public int selectCount() {
        return sysHotelMapper.selectCount();
    }
}
